package com.launcher.wkang.service;

import android.app.Service;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.util.Log;

import com.google.gson.Gson;
import com.launcher.wkang.bean.ContactBean;
import com.launcher.wkang.bean.CouListBean;
import com.launcher.wkang.util.NetUtils;
import com.launcher.wkang.util.Url;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;

public class ContactService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Map contactMap=new HashMap();
        contactMap.put("deviceSerial","1234657");
//        NetUtils.post(Url.baceUrl+"elink-app/app/contact/contactList",contactMap,contactListCallBack);
        Map browserMap=new HashMap();
        browserMap.put("deviceSerial","1234657");
//        NetUtils.post(Url.baceUrl+"elink-app/app/course/couList",browserMap,browserCallBack);
//        testAddContacts();
        return super.onStartCommand(intent, flags, startId);
    }
    private Callback contactListCallBack=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("Fragment2","IOException="+e.getMessage());

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("Fragment2","body="+response.body());
            String body=response.body().string();
//            Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, null, null, null);
            Gson gson = new Gson();

//            final ContactBean couListBean=gson.fromJson(body, ContactBean.class);
//            if (null!=couListBean){
//                if (null!=couListBean.getData()&&couListBean.getData().size()>0){
//                    for (int i=0;i<couListBean.getData().size();i++){
//                        testAddContacts(couListBean.getData().get(i).getId(),couListBean.getData().get(i).getContactName(),couListBean.getData().get(i).getContactPhone());
//                    }
//                }
//
//            }


        }
    };
    private Callback browserCallBack=new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("Fragment2","IOException="+e.getMessage());

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("Fragment2","body="+response.body());
            String body=response.body().string();
//            Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, null, null, null);
            Gson gson = new Gson();


        }
    };

    //一步一步添加数据
    public void testAddContacts(long contact_id,String name,String phone){
        //插入raw_contacts表，并获取_id属性
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();

        //插入data表
        uri = Uri.parse("content://com.android.contacts/data");
        //add Name
        values.put("raw_contact_id", contact_id);
        values.put(ContactsContract.Data.MIMETYPE,"vnd.android.cursor.item/name");
        values.put("data2", name);
        values.put("data1", name);
        resolver.insert(uri, values);
        values.clear();
        //add Phone
        values.put("raw_contact_id", contact_id);
        values.put(ContactsContract.Data.MIMETYPE,"vnd.android.cursor.item/phone_v2");
        values.put("data2", "2");   //手机
        values.put("data1", phone);
        resolver.insert(uri, values);
        values.clear();
        //add email
        values.put("raw_contact_id", contact_id);
        values.put(ContactsContract.Data.MIMETYPE,"vnd.android.cursor.item/email_v2");
        values.put("data2", "2");   //单位
        values.put("data1", "xzdong@xzdong.com");
        resolver.insert(uri, values);
    }
    public void testDelete()throws Exception{
        String name = "xzdong";
        //根据姓名求id
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID},"display_name=?", new String[]{name}, null);
        if(cursor.moveToFirst()){
            int id = cursor.getInt(0);
            //根据id删除data中的相应数据
            resolver.delete(uri, "display_name=?", new String[]{name});
            uri = Uri.parse("content://com.android.contacts/data");
            resolver.delete(uri, "raw_contact_id=?", new String[]{id+""});
        }
    }
    public void testUpdate()throws Exception{
        int id = 1;
        String phone = "999999";
        Uri uri = Uri.parse("content://com.android.contacts/data");//对data表的所有数据操作
        ContentResolver resolver = getContentResolver();
        ContentValues values = new ContentValues();
        values.put("data1", phone);
        resolver.update(uri, values, "mimetype=? and raw_contact_id=?", new String[]{"vnd.android.cursor.item/phone_v2",id+""});
    }
}
